package javolution.xml;

import com.google.android.exoplayer2.text.ttml.TtmlNode;
import javolution.Javolution;
import javolution.lang.Reusable;
import javolution.text.CharArray;
import javolution.text.TextBuilder;
import javolution.util.FastComparator;
import javolution.util.FastMap;
import javolution.util.FastTable;
import javolution.util.Index;
import javolution.xml.XMLFormat;
import javolution.xml.stream.XMLStreamException;

/* loaded from: classes2.dex */
public class XMLReferenceResolver implements Reusable {
    private int _counter;
    private FastMap _objectToId = new FastMap().setKeyComparator(FastComparator.IDENTITY);
    private FastTable _idToObject = new FastTable();
    private String _idName = TtmlNode.ATTR_ID;
    private String _idURI = null;
    private String _refName = "ref";
    private String _refURI = null;
    private TextBuilder _tmp = new TextBuilder();

    private static CharSequence toCsq(Object obj) {
        return Javolution.j2meToCharSeq(obj);
    }

    public void createReference(Object obj, XMLFormat.InputElement inputElement) throws XMLStreamException {
        CharArray attributeValue = inputElement._reader.getAttributeValue(this._idURI, this._idName);
        if (attributeValue == null) {
            return;
        }
        int i = attributeValue.toInt();
        if (this._idToObject.size() == i) {
            this._idToObject.add(obj);
            return;
        }
        throw new XMLStreamException("Identifier discontinuity detected (expected " + this._idToObject.size() + " found " + i + ")");
    }

    public Object readReference(XMLFormat.InputElement inputElement) throws XMLStreamException {
        CharArray attributeValue = inputElement._reader.getAttributeValue(this._refURI, this._refName);
        if (attributeValue == null) {
            return null;
        }
        int i = attributeValue.toInt();
        if (i < this._idToObject.size()) {
            return this._idToObject.get(i);
        }
        throw new XMLStreamException("Reference: " + ((Object) attributeValue) + " not found");
    }

    @Override // javolution.lang.Reusable
    public void reset() {
        this._idName = TtmlNode.ATTR_ID;
        this._idURI = null;
        this._refName = "ref";
        this._refURI = null;
        this._idToObject.clear();
        this._objectToId.clear();
        this._counter = 0;
    }

    public void setIdentifierAttribute(String str) {
        setIdentifierAttribute(str, null);
    }

    public void setIdentifierAttribute(String str, String str2) {
        this._idName = str;
        this._idURI = str2;
    }

    public void setReferenceAttribute(String str) {
        setReferenceAttribute(str, null);
    }

    public void setReferenceAttribute(String str, String str2) {
        this._refName = str;
        this._refURI = str2;
    }

    public boolean writeReference(Object obj, XMLFormat.OutputElement outputElement) throws XMLStreamException {
        Index index = (Index) this._objectToId.get(obj);
        if (index != null) {
            this._tmp.clear().append(index.intValue());
            if (this._refURI == null) {
                outputElement._writer.writeAttribute(this._refName, this._tmp);
                return true;
            }
            outputElement._writer.writeAttribute(this._refURI, this._refName, this._tmp);
            return true;
        }
        int i = this._counter;
        this._counter = i + 1;
        Index valueOf = Index.valueOf(i);
        this._objectToId.put(obj, valueOf);
        this._tmp.clear().append(valueOf.intValue());
        if (this._idURI == null) {
            outputElement.getStreamWriter().writeAttribute(this._idName, this._tmp);
            return false;
        }
        outputElement.getStreamWriter().writeAttribute(this._idURI, this._idName, this._tmp);
        return false;
    }
}
